<?php
class requeriments_information extends ArrayIterator
{
	public $compatible_version = true;
	public $compatible_exist = true;
	public $other_data = '';
	public function current()
	{
		$this->other_data = '';
		$current =  parent::current();
		$current = $this->_parts_or($current);
		if(count($current) < 2)
		{
			$n = $this->_parts_version($current[0]);
			$current = $n[0];
			$this->other_data = $n[1];
		}
		return $current;
	}
	private function _parts_or($value)
	{
		return explode("||" , $value);
	}
	private function _parts_version($string)
	{
		if(strpos($string, ':') !== false)
		{
			$d = explode(':',$string,2);
		}
		else
		{
			$d = array($string, 0 );
		}
		return array($d[0],$d[1]);
	}
}

/**
 * @property  path_admin_plugin
 */
class requeriments
{
	public $class = array();
	public $module = array();
	public $plugin = array();
	public $component = array();
	public $function = array();
	public $extension = array();
	public $library = array();
	public $php_ini = array();
	private $path_component;
	private $path_admin_component;
	private $path_module;
	private $path_admin_module;
	private $path_plugin;
	public function __construct($file)
	{
		if(file_exists($file))
		{
			$json = json_decode(file_get_contents($file),true);
			if(is_array($json))
			{
				$this->class = isset($json['class']) ? new requeriments_information($json['class']) : array();
				$this->module = isset($json['module']) ? new requeriments_information($json['module']) : array();
				$this->plugin = isset($json['plugin']) ? new requeriments_information($json['plugin']) : array();
				$this->component_admin = isset($json['component_admin']) ? new requeriments_information($json['component_admin']) : array();
				$this->component = isset($json['component']) ? new requeriments_information($json['component']) : array();
				$this->function = isset($json['function']) ? new requeriments_information($json['function']) : array();
				$this->extension = isset($json['extension']) ? new requeriments_information($json['extension']) : array();
				$this->library = isset($json['library']) ? new requeriments_information($json['library']) : array();
				$this->php_ini = isset($json['php_ini']) ? new requeriments_information($json['php_ini']) : array();
			}
		}
	}
	public function php_ini_compare(& $valor, &$simbolo,$dato_a_comparar)
	{
		$datos = array();
		//$retorna = false;
		if(preg_match('/([a-zA-Z_0-9?&;\/#\.]+)(&&|\|\||==|<=|>=|<>|\!=|<|>)/is',
		$this->php_ini->other_data,$datos))
		{
			$datos[0]= $dato_a_comparar;
			if(!$this->_php_ini_equal_true_false($datos,$retorna))
			{
				if(!$this->_php_ini_equal_size($datos,$retorna))
				{
					$retorna = $this->_return_equal($datos[1],$datos[2],$datos[0]);
				}
			}
		}
		$valor = $datos[1];
		$simbolo = $datos[2];
		return $retorna;
	}
	private function _php_ini_equal_true_false(&$data, & $return)
	{
		$estado1 = $data[1];
			$e1 = false;

		$estado2 = $data[0];
			$e2 = false;
			$estado1 = $this->_return_boolean($estado1, $e1);
			$estado2 = $this->_return_boolean($estado2, $e2);
			if($estado1 === false || $estado2 === false )
			{
				return false;
			}
			$return = $this->_return_equal($e1,$data[2],$e2);

		return true;
	}
	private function _php_ini_equal_size(& $data,& $retorna)
	{
		list($size,$magnitud)=sscanf($data[1], "%d%s");
		if($size == null ||  $magnitud == null )
		{
			return false;
		}
		else
		{
			$bytes1 = $this->_return_bytes($size,$magnitud);
			list($size2,$magnitud2)=sscanf($data[0], "%d%s");
			$bytes2 = $this->_return_bytes($size2,$magnitud2);

			if($bytes1 === false)
			{
				return false;
			}
			$retorna = $this->_return_equal($bytes1,$data[2],$bytes2);
			return true;
		}
	}

	private function _return_boolean($string,& $return)
	{
		switch(strtolower($string))
		{
			case 'off':
			case '0':
			case 'false':
			case '':
			$return = 'off';break;
			case 'on':
			case '1':
			case 'true':
			$return = 'on';break;
			default:
			return false;
		}
		return true;
	}
	private function _return_equal($data1,$symbol,$data2)
	{
		$data1 = is_numeric($data1)? $data1 : "'$data1'";
		$data2 = is_numeric($data2)? $data2 : "'$data2'";
		switch($symbol)
		{
			case '==':
			case '=':
			return $data1 == $data2 ;
			case '<=':
			return $data1 <= $data2 ;
			case '<':
			return $data1 < $data2 ;
			case '>':
			return $data1 > $data2 ;
			case '<>':
			return $data1 <> $data2 ;
			case '!=':
			return $data1 != $data2 ;
			default:
			return false;
		}
	}
	private function _return_bytes($size,$magnitud)
	{
		$size = (float)$size;
		switch($magnitud)
		{
		        case 't':
		        case 'T':
		        	$size *= 1024;
		        case 'g':
		        case 'G':
		            $size *= 1024;
		        case 'm':
		        case 'M':
		            $size *= 1024;
		        case 'k':
		        case 'K':
		            $size *= 1024;
		            break;
          		default:return false;
		    }
		return $size;
	}
	public function set_path_admin_component($path)
	{
		$this->path_admin_component = $path;
	}
	public function set_path_component($path)
	{
		$this->path_component = $path;
	}
	public function set_path_admin_module($path)
	{
		$this->path_admin_module = $path;
	}
	public function set_path_module($path)
	{
		$this->path_module = $path;
	}
	public function set_path_plugin($path)
	{
		$this->path_admin_plugin = $path;
	}
	public function compare_version(& $current,$path_json)
	{
		$name = $current->current();
		if(preg_match('/([a-zA-Z_0-9?&;\/#\.]+)(==|<=|>=|<>|\!=|<|>|=)/is',$current->other_data,$datos))
		{
			if(file_exists($path_json))
			{
				$json = json_decode(file_get_contents($path_json) ,true );
				var_dump($datos);
				if(version_compare($datos[0],$json["version"],$datos[1]) )
				{
					return true;
				}
				return false;
			}
			else
			{
				return false;
			}
		}
		return false;
	}
}